package com.itheima.web.controller;

import cn.hutool.core.util.NumberUtil;
import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    @GetMapping("/manage/users")
    public ResponseEntity findByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize){

        return userManager.findByPage(pageNum,pageSize);
    }

    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId){
//                userManager.iff(userId);
        return userManager.findUserInfoById(userId);
    }

    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByCondition4Page(
            Long uid,
            String state,
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize){

        Integer status = NumberUtil.isInteger(state)?NumberUtil.parseInt(state):null;

        return userManager.findMovementByCondition4Page(uid,status,pageNum,pageSize);
    }

    @GetMapping("/manage/messages/{movementId}")
    public ResponseEntity findMovementById(@PathVariable String movementId){
        return userManager.findMovementById(movementId);
    }


    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementComemntByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize,
            String messageID){
        return userManager.findMovementComemntByPage(pageNum,pageSize,messageID);
    }

    @GetMapping("/manage/videos")
    public ResponseEntity findUserVideoByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")int pageSize,
            Long uid){
        return userManager.findUserVideoByPage(pageNum,pageSize,uid);
    }

    @PostMapping("/manage/users/freeze")
    public ResponseEntity saveFreeze(@RequestBody Map<String,String> map){

        int userId = Integer.parseInt(map.get("userId"));
        int freezingTime = Integer.parseInt(map.get("freezingTime"));
        String freezingRange = map.get("freezingRange");
        String reasonsForFreezing = map.get("reasonsForFreezing");
        String frozenRemarks = map.get("frozenRemarks");

        return ResponseEntity.ok(userManager.saveFreeze(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks));
    }
    @PostMapping("/manage/users/unfreeze")
    public ResponseEntity unSaveFreeze(@RequestBody Map<String,String> map){
        //用户id
        int userId = Integer.parseInt(map.get("userId"));
        //解冻原因
//        String reasonsForThawing = map.get("reasonsForThawing");

        return ResponseEntity.ok(userManager.unSaveFreeze(userId));
    }



}
